package edu.univercidade.tcc.sap.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name="users")
@NamedQueries({
		@NamedQuery(name = "usersByStatus", 
				query = "SELECT u " +
						"FROM User u " +
						"WHERE u.status = :status"),
		@NamedQuery(name = "userByLogin", 
				query = "SELECT u " +
						"FROM User u " +
						"WHERE u.login = :login") })
public class User implements Serializable {

	private static final long serialVersionUID = -1268672325469316049L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(unique=true, nullable=false)
	private int id;
	
	@Enumerated(EnumType.ORDINAL)
	@Column(nullable=false)
	private BooleanType admin;
/*
	@Column(name="auth_source_id")
	private int authSourceId;
*/
    @Temporal( TemporalType.TIMESTAMP)
	@Column(name="created_on")
	private Date createdOn;

	@Column(nullable=false, length=30)
	private String firstname;

	@Column(name="hashed_password", nullable=false, length=40)
	private String hashedPassword;

	@Column(name="identity_url", length=255)
	private String identityUrl;

	@Column(length=5)
	private String language;

    @Temporal( TemporalType.TIMESTAMP)
	@Column(name="last_login_on")
	private Date lastLoginOn;

	@Column(nullable=false, length=30)
	private String lastname;

	@Column(nullable=false, length=30)
	private String login;

	@Column(nullable=false, length=60)
	private String mail;

	@Column(name="mail_notification", nullable=false, length=255)
	@Enumerated(EnumType.STRING)
	private EmailNotification mailNotification;

	@Column(nullable=false)
	@Enumerated(EnumType.ORDINAL)
	private UserStatus status;

	@Column(length=255)
	private String type;

    @Temporal( TemporalType.TIMESTAMP)
	@Column(name="updated_on")
	private Date updatedOn;

	//bi-directional many-to-many association to User
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
		name="groups_users"
		, joinColumns={
			@JoinColumn(name="group_id", nullable=false)
			}
		, inverseJoinColumns={
			@JoinColumn(name="user_id", nullable=false)
			}
		)
	private final List<User> users1 = new ArrayList<User>();

	//bi-directional many-to-many association to User
	@ManyToMany(mappedBy="users1", fetch=FetchType.EAGER)
	private final List<User> users2 = new ArrayList<User>();

    public User() {
    }

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public BooleanType isAdmin() {
		return this.admin;
	}
	
	public void setAdmin(BooleanType admin) {
		this.admin = admin;
	}

/*
	public int getAuthSourceId() {
		return this.authSourceId;
	}

	public void setAuthSourceId(int authSourceId) {
		this.authSourceId = authSourceId;
	}
*/
	public Date getCreatedOn() {
		return this.createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public String getFirstname() {
		return this.firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getHashedPassword() {
		return this.hashedPassword;
	}

	public void setHashedPassword(String hashedPassword) {
		this.hashedPassword = hashedPassword;
	}

	public String getIdentityUrl() {
		return this.identityUrl;
	}

	public void setIdentityUrl(String identityUrl) {
		this.identityUrl = identityUrl;
	}

	public String getLanguage() {
		return this.language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public Date getLastLoginOn() {
		return this.lastLoginOn;
	}

	public void setLastLoginOn(Date lastLoginOn) {
		this.lastLoginOn = lastLoginOn;
	}

	public String getLastname() {
		return this.lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getLogin() {
		return this.login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getMail() {
		return this.mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public EmailNotification getMailNotification() {
		return this.mailNotification;
	}

	public void setMailNotification(EmailNotification mailNotification) {
		this.mailNotification = mailNotification;
	}

	public UserStatus getStatus() {
		return this.status;
	}

	public void setStatus(UserStatus status) {
		this.status = status;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Date getUpdatedOn() {
		return this.updatedOn;
	}

	public void setUpdatedOn(Date updatedOn) {
		this.updatedOn = updatedOn;
	}

	public List<User> getUsers1() {
		return this.users1;
	}

	void setUsers1(List<User> users1) {
		this.users1.clear();
		this.users1.addAll(users1);
	}
	
	public List<User> getUsers2() {
		return this.users2;
	}

	void setUsers2(List<User> users2) {
		this.users2.clear();
		this.users2.addAll(users2);
	}

}